<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-pie-chart"></i> 商品类别
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <el-button type="primary" @click="addCategory()">新增商品类别</el-button>
      <el-table :data="categoryList" style="width: 70%">
        <el-table-column
          fixed
          prop="categoryId"
          label="商品类别ID"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="categoryName"
          label="类别名称"
          width="200"
        ></el-table-column>
        <el-table-column
          prop="categorySort"
          label="类别排序"
          width="140"
        ></el-table-column>
        <el-table-column label="类别状态" width="180">
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.categoryState === '0'"
              >正常</el-tag
            >
            <el-tag type="info" v-if="scope.row.categoryState === '1'"
              >下架</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="250">
          <template #default="scope">
            <el-button type="primary" @click="editCategory(scope.row)"
              >编辑</el-button
            >
            <el-button type="primary" @click="removeCategory(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-dialog title="修改商品类别" v-model="CategoryDetailedVisible">
        <el-form ref="form" :model="category" label-width="70px">
          <el-form-item label="类别名称" label-width="100">
            <el-input
              v-model="category.categoryName"
              placeholder="请输入内容"
            ></el-input>
          </el-form-item>
          <el-form-item label="类别状态(0:正常   1:下架)" label-width="100">
            <el-input
              v-model="category.categoryState"
              placeholder="请输入内容"
            ></el-input>
          </el-form-item>
          <el-form-item label="类别排序(数值越大)类别越靠前" label-width="100">
            <el-input
              v-model="category.categorySort"
              placeholder="请输入内容"
              width="100px"
            ></el-input>
          </el-form-item>
        </el-form>
        <span class="dialog-footer">
          <el-button @click="CategoryDetailedVisible = false">取 消</el-button>
          <el-button type="primary" @click="editCategorySave">确 定</el-button>
        </span>
      </el-dialog>
      <el-dialog title="新增商品类别" v-model="addCategoryDetailedVisible">
        <el-form ref="form" :model="newCategory" label-width="70px">
          <el-form-item label="类别名称" label-width="100">
            <el-input
              v-model="newCategory.categoryName"
              placeholder="请输入内容"
            ></el-input>
          </el-form-item>
          <el-form-item label="类别状态(0:正常   1:下架)" label-width="100">
            <el-input
              v-model="newCategory.categoryState"
              placeholder="请输入内容"
            ></el-input>
          </el-form-item>
          <el-form-item label="类别排序(数值越大)类别越靠前" label-width="100">
            <el-input
              v-model="newCategory.categorySort"
              placeholder="请输入内容"
              width="100px"
            ></el-input>
          </el-form-item>
        </el-form>
        <span class="dialog-footer">
          <el-button @click="addCategoryDetailedVisible = false"
            >取 消</el-button
          >
          <el-button type="primary" @click="addCategorySave">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import request from "../api/index";
export default {
  name: "CfxsAdminCategory",
  components: {},
  directives: {},
  data() {
    return {
      categoryList: [],
      CategoryDetailedVisible: false,
      addCategoryDetailedVisible: false,
      category: {},
      newCategory: {
        categoryName: "",
        categoryState: "",
        categorySort: "",
      },
    };
  },
  mounted() {
    this.getCategoryList();
  },
  methods: {
    getCategoryList() {
      request.getCategoryList().then((res) => {
        console.log(res);
        if (res.code === 200) {
          this.categoryList = res.data;
          this.$message.success("获取商品类别成功");
        } else {
          this.$message.error("获取商品类别失败");
        }
      });
    },
    editCategory(row) {
      this.CategoryDetailedVisible = true;
      this.category = row;
    },
    removeCategory(row) {
      this.$confirm("确定要删除吗？", "提示", {
        type: "warning",
      }).then(() => {
        let query = {
          categoryId: row.categoryId,
        };
        request.removeCategory(query).then((res) => {
          if (res.code === 200) {
            this.$message.success("删除成功");
            this.getCategoryList();
          } else {
            this.$$message.error("删除失败");
          }
        });
      });
    },
    editCategorySave() {
      let category = {
        category: this.category,
      };
      request.editCategory(category).then((res) => {
        if (res.code === 200) {
          this.$message.success("修改成功");
          this.editCategorySave = false;
          this.getCategoryList();
        }
      });
    },
    addCategory() {
      this.addCategoryDetailedVisible = true;
    },
    addCategorySave() {
      let query = {
        category: this.newCategory,
      };
      request.addCategory(query).then((res) => {
        if (res.code === 200) {
          this.$message.success("新增商品类别成功");
          this.addCategoryDetailedVisible = false;
          this.getCategoryList();
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
</style>